package com.madou.dbtool.redis.node;


import com.madou.common.annotation.HideClass;
import com.madou.dbtool.common.enums.DBEnum;
import com.madou.dbtool.common.icon.AllIcons;
import com.madou.dbtool.common.messages.DBToolBundle;
import com.madou.dbtool.redis.manager.RedisNodeInfo;
import com.madou.dbtool.redis.manager.RedisSourceManager;
import com.madou.dbtool.relationship.common.BaseNonRelationNodeParm;
import com.madou.dbtool.relationship.node.menu.NodeMenuFactory;
import com.madou.dbtool.ui.tree.DataSourceNode;

import javax.swing.*;
import javax.swing.tree.TreePath;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author miukoo
 * Redis服务器组信息节点
 */
@HideClass
public class RedisServerGroupNode extends BaseNonRelationNodeParm {
    //表的菜单
    private static List<JMenuItem> REDIS_NODE_MENU = new ArrayList<>();
    static {
        REDIS_NODE_MENU.add(createMenu(DBToolBundle.message("ds.tree.redis.server.node.reload"),114,114, AllIcons.MENU.REFRESH));
    }
    // 数据库类型
    DBEnum type = DBEnum.REDIS;
    // 资源管理器
    RedisSourceManager redisSourceManager;
    // 节点新
    List<RedisNodeInfo> nodes;
    Map<Integer,RedisServerNode> cache = new HashMap<>();

    public RedisServerGroupNode(DataSourceNode parent, RedisSourceManager redisSourceManager) {
        super(parent);
        this.redisSourceManager = redisSourceManager;
        if(!open) {
            nodes = redisSourceManager.getNodes();
            open = true;
        }
    }

    @Override
    public DataSourceNode getChildren(DataSourceNode parent, int index) {
        RedisServerNode redisServerNode = cache.get(index);
        if(redisServerNode==null){
            redisServerNode = new RedisServerNode(this,nodes.get(index));
            cache.put(index,redisServerNode);
        }
        return redisServerNode;
    }

    @Override
    public String getText() {
        return "nodes-"+(redisSourceManager.isCluster()?"cluster":"standalone");
    }

    @Override
    public Icon getIcon() {
        return AllIcons.Common.NODE_GROUP;
    }

    @Override
    public int countChild() {
        return nodes.size();
    }

    @Override
    public boolean hasChild() {
        return true;
    }

    /**
     * 子元素右键菜单点击
     * @return
     */
    @Override
    public void clickMenu(JTree jTree, TreePath treePath, JMenuItem jMenuItem){
        // 广播事件
        if(isMenuCode(jMenuItem,1)){
            reload(jTree,treePath);
        }
    }

    @Override
    public void reload(JTree jTree, TreePath treePath) {
        open=false;
        cache.clear();
        super.reload(jTree,treePath);
    }

    /**
     * 子元素右键菜单
     * @return
     */
    @Override
    public List<JMenuItem> rightMenu(){
        List temp = new ArrayList();
        for (JMenuItem dbMenu : REDIS_NODE_MENU) {
            temp.add(NodeMenuFactory.copyMenuItem(dbMenu));
        }
        return temp;
    }

    /**
     * 是否自动展开
     * @return
     */
    @Override
    public boolean autoExpand(){
        return true;
    }

}
